package com.example.demo.core.utils;

import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;

public class BeanConvertor {
    public static <T> T transfer(Object source, T result) {
        if (source == null) {
            return null;
        }
        BeanUtils.copyProperties(source,result);
        return result;
    }

    public static <T> T transfer(Object source, BeanFactory<T> factory) {
        if (source == null) {
            return null;
        }
        T result = factory.make();
        BeanUtils.copyProperties(source,result);
        return result;
    }

    public static <T, S> List<T> transfer(List<S> list, BeanFactory<T> factory) {
        List<T> result = new ArrayList<>();
        for(int i=0;i<list.size();i++) {
            T t = factory.make();
            BeanUtils.copyProperties(list.get(i), t);
            result.add(t);
        }
        return result;
    }
}
